package com.llkj.qingnianguoxue.circle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.ContributionsListAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.ContributionsBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ContributionsListActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.refresh_layout)
    protected SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    protected RecyclerView lvContributionList;
    private ContributionsListAdapter mContributionAdapter;//适配器
    private List<ContributionsBean> mContributionData;
    private int totalSize, mPage,mCurrentPosition;
    private String mGroupChatId;//群聊天id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contributions_list);
        setTitle("缴费明细", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1,-1);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        mGroupChatId = intent.getStringExtra("groupChatId");

        initView();

        loadWaitDialog();
        onRefresh();
    }

    private void initView(){
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvContributionList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        mContributionData = new ArrayList<>();
        mContributionAdapter = new ContributionsListAdapter(R.layout.item_contributions_list,mContributionData);
        lvContributionList.setAdapter(mContributionAdapter);

        refreshLayout.setOnRefreshListener(this);
        mContributionAdapter.setOnLoadMoreListener(this);
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getData();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getData();
    }

    private void getData(){
        HttpMethod.circleConsumption(this,this,mGroupChatId,String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_CIRCLE_CONSUMPTION) {
            ResponseList<ContributionsBean> responseList = GsonUtil.fromJsonList(data,ContributionsBean.class);
            if(responseList!=null){
                totalSize = Integer.parseInt(responseList.getNum());
                if (mPage == 1) {
                    mContributionData.clear();
                    LoadDataHelper.refreshComplete(mContributionAdapter, mContributionData, refreshLayout, lvContributionList);
                } else {
                    mContributionAdapter.setEnableLoadMore(true);
                    mContributionAdapter.loadMoreComplete();
                }

                mContributionData.addAll(responseList.getConsumption());
                mContributionAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, mContributionAdapter, null, lvContributionList, totalSize);
    }
}
